package com.ecommerce.common.core;

import com.ecommerce.common.enums.ApiResultCodeEnum;
import lombok.Data;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.UUID;

// 统一返回结果
@Data
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String msg;
    private T data;
    private Timestamp timestamp;
    private String requestId;
    public ApiResult() {
        this.timestamp = new Timestamp(System.currentTimeMillis());
        this.requestId = UUID.randomUUID().toString();
    }
    public ApiResult(Integer code, String msg, T data) {
        this();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResult<T> success(String msg, T data) {
        return new ApiResult<>(ApiResultCodeEnum.SUCCESS.getCode(), msg, data);
    }
    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ApiResultCodeEnum.SUCCESS.getCode(), ApiResultCodeEnum.SUCCESS.getMsg(), data);
    }
    public static <T> ApiResult<T> success(String msg) {
        return new ApiResult<>(ApiResultCodeEnum.SUCCESS.getCode(), msg, null);
    }
    public static <T> ApiResult<T> success() {
        return new ApiResult<>(ApiResultCodeEnum.SUCCESS.getCode(), ApiResultCodeEnum.SUCCESS.getMsg(), null);
    }
    public static <T> ApiResult<T> error(Integer code, String msg) {
        return new ApiResult<>(code, msg, null);
    }
    public static <T> ApiResult<T> error(String msg) {
        return new ApiResult<>(ApiResultCodeEnum.FAIL.getCode(), msg, null);
    }
    public static <T> ApiResult<T> fail(String msg) {
        return new ApiResult<>(ApiResultCodeEnum.FAIL.getCode(), msg, null);
    }
    public static <T> ApiResult<T> fail() {
        return new ApiResult<>(ApiResultCodeEnum.FAIL.getCode(), ApiResultCodeEnum.FAIL.getMsg(), null);
    }
    public static <T> ApiResult<T> fail(Integer code, String msg) {
        return new ApiResult<>(code, msg, null);
    }
    public static <T> ApiResult<T> badRequest() {
        return new ApiResult<>(ApiResultCodeEnum.BADREQUEST.getCode(), ApiResultCodeEnum.BADREQUEST.getMsg(), null);
    }
    public static <T> ApiResult<T> badRequest(String msg) {
        return new ApiResult<>(ApiResultCodeEnum.BADREQUEST.getCode(), msg, null);
    }
    public static <T> ApiResult<T> unauthorized() {
        return new ApiResult<>(ApiResultCodeEnum.UNAUTHORIZED.getCode(), ApiResultCodeEnum.UNAUTHORIZED.getMsg(), null);
    }
    public static <T> ApiResult<T> forbidden() {
        return new ApiResult<>(ApiResultCodeEnum.FORBIDDEN.getCode(), ApiResultCodeEnum.FORBIDDEN.getMsg(), null);
    }
    public static <T> ApiResult<T> forbidden(String msg) {
        return new ApiResult<>(ApiResultCodeEnum.FORBIDDEN.getCode(), msg, null);
    }
    public static <T> ApiResult<T> notFound() {
        return new ApiResult<>(ApiResultCodeEnum.NOTFOUND.getCode(), ApiResultCodeEnum.NOTFOUND.getMsg(), null);
    }
    public boolean isSuccess() {
        return this.code == ApiResultCodeEnum.SUCCESS.getCode();
    }
}
